package com.lib.application;

import com.lib.dto.InstallmentInfoDTO;
import com.lib.dto.InstallmentRequestDTO;
import com.lib.dto.enums.CustomerErrorEnum;
import com.lib.dto.mapper.InstallmentInfoMapper;
import com.lib.model.*;
import com.lib.model.enums.InstallmentRequestingStatus;
import com.lib.model.enums.InstallmentStatusEnum;
import com.lib.repository.InstallmentInfoRepository;
import com.lib.repository.InstallmentRequestRepository;
import com.lib.repository.RentalAgentRepository;
import com.lib.repository.WxAccountReponsitory;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by duqingxiang on 17/5/25.
 */
@Service
public class InstallmentRequestService {

    @Autowired
    WxAccountReponsitory wxAccountReponsitory;
    @Autowired
    InstallmentRequestRepository installmentRequestRepository;
    @Autowired
    InstallmentInfoRepository installmentInfoRepository;
    @Autowired
    RentalAgentRepository rentalAgentRepository;

    public InstallmentRequestDTO getNormalRequest(Customer customer,long agentId) {


        List<InstallmentRequest> customerRequests = installmentRequestRepository.findInstallmentRequestByCustomerIdAndStatusIn(customer.getId(), InstallmentRequestingStatus.STATUS_LIST);
        if (!CollectionUtils.isEmpty(customerRequests)) {
            return new InstallmentRequestDTO(CustomerErrorEnum.REPEAT_REQUEST.getKey());
        }

        InstallmentRequest request = installmentRequestRepository.findInstallmentRequestByCustomerIdAndAgentIdAndStatusGreaterThanEqual(customer.getId(),agentId,0);
        if (request == null) {

            InstallmentRequestDTO.InstallmentRequestDTOBuilder builder = InstallmentRequestDTO.builder();

            RentalAgent agent = rentalAgentRepository.findOne(agentId);
            if (agent == null) {
                return new InstallmentRequestDTO(-2);
            }

            builder.customerId(customer.getId())
                    .agentId(agentId);
            if (agent != null) {
                builder.agentName(agent.getName());
            }

            return builder.build();
        }


        return this.getRequestDTO(request);
    }

    public InstallmentRequestDTO getRequestDTO(InstallmentRequest request) {
        InstallmentRequestDTO.InstallmentRequestDTOBuilder builder = InstallmentRequestDTO.builder();

        builder.id(request.getId())
                .customerId(request.getCustomerId())
                .agentId(request.getAgentId())
                .infoId(request.getInfoId())
                .phone(request.getPhone())
                .monthlyRent(request.getMonthlyRent())
                .rentalPeriod(request.getRentalPeriod())
                .startDate(request.getStartDate())
                .status(InstallmentStatusEnum.valueOf(request.getStatus()))
                .createdDate(request.getCreatedDate());


        RentalAgent agent = rentalAgentRepository.findOne(request.getAgentId());
        if (agent != null) {
            builder.agentName(agent.getName());
        }

        InstallmentInfo info = installmentInfoRepository.findOne(request.getInfoId());
        if (info != null) {
            InstallmentInfoMapper infoMapper = Mappers.getMapper(InstallmentInfoMapper.class);
            InstallmentInfoDTO infoDTO = infoMapper.convert(info);
            builder.infoDTO(infoDTO);
        }

        return builder.build();
    }

}
